package com.ymatou.query.seller.trading.api.parameter;

import java.util.Map;

import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

/**
 * 存储过程: 交易服务SP->sp_trd_GetSellerSalesDetail_v2
 * 
 * @author songyefeng
 *
 */
@SuppressWarnings("rawtypes")
public class Sp_GetSellerSalesDetail_v2 {
	private int sellerId;
	private String date = null;

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public void setDate(String date) {
		this.date = date;
	}

	private String getQueryField(String day) {
		StringBuffer sb = new StringBuffer();
		sb.append(" '" + day + "' as TimeSpan,");
		// 下面3个是未付款
		sb.append(" sum(case s.fPaidAmountOfCash when 0 then IIF(i.iSalesType = 1, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0) else 0 end) Unpaid_Spot,");
		sb.append(" sum(case s.fPaidAmountOfCash when 0 then IIF(i.iSalesType = 2 or i.iSalesType is null, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0) else 0 end) Unpaid_Shangou,");
		sb.append(" sum(case s.fPaidAmountOfCash when 0 then isnull(i.fSellerCouponAmount,0) else 0 end) Unpaid_SellerCoupon,");
		// 下面3个是已付定金
		sb.append(" sum(case when o.bShangouOrder = 1 and o.bPaidInFull = 0 and s.fPaidAmountOfCash > 0 and s.fPostPaidAmountOfCash = 0 then IIF(i.iSalesType = 1, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0) else 0 end) PartialPaid_Spot,");
		sb.append(" sum(case when o.bShangouOrder = 1 and o.bPaidInFull = 0 and s.fPaidAmountOfCash > 0 and s.fPostPaidAmountOfCash = 0 then IIF(i.iSalesType = 2 or i.iSalesType is null, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0) else 0 end) PartialPaid_Shangou,");
		sb.append(" sum(case when o.bShangouOrder = 1 and o.bPaidInFull = 0 and s.fPaidAmountOfCash > 0 and s.fPostPaidAmountOfCash = 0 then isnull(i.fSellerCouponAmount,0) else 0 end) PartialPaid_SellerCoupon,");
		// 下面3个是已付全款
		sb.append(" sum(case when o.bPaidInFull = 1 and s.fPaidAmountOfCash > 0 then IIF(i.iSalesType = 1, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0) else 0 end) FullPaid_Spot,");
		sb.append(" sum(case when o.bPaidInFull = 1 and s.fPaidAmountOfCash > 0 then IIF(i.iSalesType = 2 or i.iSalesType is null, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0) else 0 end) FullPaid_Shangou,");
		sb.append(" sum(case when o.bPaidInFull = 1 and s.fPaidAmountOfCash > 0 then isnull(i.fSellerCouponAmount,0) else 0 end) FullPaid_SellerCoupon,");
		// 下面3个是已取消 12,13,18
		sb.append(" sum(case when o.iTradingStatus in (12,13,18) then IIF(i.iSalesType = 1, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0) else 0 end) Cancelled_Spot,");
		sb.append(" sum(case when o.iTradingStatus in (12,13,18) then IIF(i.iSalesType = 2 or i.iSalesType is null, i.fProductPrice * i.iAmount - isnull(i.fSellerCouponAmount,0) - isnull(i.fSellerPromotionAmount,0), 0) else 0 end) Cancelled_Shangou,");
		sb.append(" sum(case when o.iTradingStatus in (12,13,18) then isnull(i.fSellerCouponAmount,0) else 0 end) Cancelled_SellerCoupon");

		return sb.toString();
	}

	private String getJoinCondtion() {
		return " join ymt_orderstate(nolock) s on o.iOrderId = s.iOrderId join ymt_orderinfo(nolock) i on i.iOrderId = o.iOrderId";
	}

	private String getQueryCondition(String begin, String end) {
		return " o.iBuyerId = " + sellerId + " and o.dAddTime >= '" + begin
				+ "' and o.dAddTime < '" + end + "'";
	}

	public void execute() {
		YmtOrdersWapper wapper = new YmtOrdersWapper();

		String format = YMTDateUtil.YMD;
		if (DataUtil.Stringg.isNullOrEmpty(date)) {
			date = YMTDateUtil.getSysTime();
		}
		String today = YMTDateUtil.addDays(0, format);
		String yesterday = YMTDateUtil.addDays(-1, format);
		String tomorrow = YMTDateUtil.addDays(1, format);
		String week = YMTDateUtil.calendarToString(
				com.ymttest.common.entitie.TimeRange.getFirstDayOfThisWeek(),
				format);
		String month = YMTDateUtil.calendarToString(
				com.ymttest.common.entitie.TimeRange.getFirstDateOfThisMonth(),
				format);
		this.yesterday = wapper.selectOrders2(getQueryField("yesterday"),
				getJoinCondtion(), getQueryCondition(yesterday, today)).get(0);
		this.today = wapper.selectOrders2(getQueryField("today"),
				getJoinCondtion(), getQueryCondition(today, tomorrow)).get(0);
		this.week = wapper.selectOrders2(getQueryField("week"),
				getJoinCondtion(), getQueryCondition(week, tomorrow)).get(0);
		this.month = wapper.selectOrders2(getQueryField("month"),
				getJoinCondtion(), getQueryCondition(month, tomorrow)).get(0);
	}

	private Map yesterday;
	private Map today;
	private Map week;
	private Map month;

	public Map getYesterday() {
		return yesterday;
	}

	public Map getToday() {
		return today;
	}

	public Map getWeek() {
		return week;
	}

	public Map getMonth() {
		return month;
	}

	public static void main(String[] args) {
		Sp_GetSellerSalesDetail_v2 sp = new Sp_GetSellerSalesDetail_v2();
		sp.setSellerId(3383);
		sp.execute();
	}

}
